# 电子病历管理的视图文件

from django.shortcuts import render, redirect
from common import models


def index(request):
    '''浏览信息'''
    umod = models.Hscinfo.objects
    ulist = umod.all()
    context = {"hscinfolist": ulist}
    return render(request, "myapp/hscinfo/index.html", context)

def search(request):
    # 从表单中获取要添加的信息并封装到obj对象中
    time = request.POST['time']
    obj = models.Hscinfo.objects.get(time=time)
    context = {'hscinfo': obj}
    return render(request, "myapp/hscinfo/edit.html",context)


# def add(request):
#     '''加载信息添加表单'''
#     if request.method == "GET":
#         return render(request, "myapp/hscinfo/add.html")
#     # 获取用户POST提交过来的数据
#     request.POST.get("templateid", "zhusu", "xianbingshi", "jiwangshi", "hunyushi", "yuejingshi", "jiazushi", "tigejiancha",
#                      "zhuankejiancha", "fuzhujiancha", "jianbiezhenduan", "chubuzhenduan", "zhenliaojihua")
#     # 保存到数据库
#     models.Templateinfo.objects.create()
#
#     return redirect("Templateinfoinfo/")


# def insert(request):
#     '''执行信息添加'''
#     try:
#         obj = Templateinfo()
#         # 从表单中获取要添加的信息并封装到ob对象中
#         obj.templateid = request.POST['templateid']
#         obj.zhusu = request.POST['zhusu']
#         obj.xianbingshi = request.POST['xianbingshi']
#         obj.jiwangshi = request.POST['jiwangshi']
#         obj.hunyushi = request.POST['hunyushi']
#         obj.yuejingshi = request.POST['yuejingshi']
#         obj.jiazushi = request.POST['jiazushi']
#         obj.tigejiancha = request.POST['tigejiancha']
#         obj.zhuankejiancha = request.POST['zhuankejiancha']
#         obj.fuzhujiancha = request.POST['fuzhujiancha']
#         obj.jianbiezhenduan = request.POST['jianbiezhenduan']
#         obj.chubuzhenduan = request.POST['chubuzhenduan']
#         obj.zhenliaojihua = request.POST['zhenliaojihua']
#         obj.save()
#         context = {'info': "添加成功！"}
#     except Exception as err:
#         print(err)
#         context = {'info': "添加失败！"}
#     return render(request, "myapp/info.html", context)


def delete(request,nid):
    try:
        obj = models.Hscinfo.objects.get(time=nid)
        obj.delete()
        context = {'info':"删除成功"}
    except Exception as err:
        print(err)
        context = {'info':"删除失败"}
    return render(request,"myapp/info.html",context)



# def edit(request,templateid):
#     '''加载信息编辑表单'''
#     try:
#         obj = Templateinfo.objects.get(templateid=templateid)
#         context = {'Templateinfo':obj}
#         return render(request, "myapp/Templateinfo/edit.html",context)
#     except Exception as err:
#         print(err)
#         context = {'info':"没有找到要修改的信息！"}
#         return render(request,"myapp/info.html",context)


def update(request):
    '''执行信息编辑'''
    try:
        time = request.POST['time']
        obj = models.Hscinfo.objects.get(time=time)
        obj.name = request.POST['name']
        obj.heartrate = request.POST['heartrate']
        obj.spo2 = request.POST['spO2']
        obj.save()
        context = {'info':"修改成功！"}
    except Exception as err:
        print(err)
        context = {'info':"修改失败！"}
    return render(request,"myapp/info.html",context)

